extension DateExtension on DateTime {
  static DateTime? createDateTime(dynamic value) {
    if (value == null) {
      return null;
    }

    if (value is String) {
      return DateTime.parse(value);
    } else {
      return DateTime.fromMillisecondsSinceEpoch(
        value.toInt(),
        isUtc: false,
      );
    }
  }

  String get dateDescription {
    var now = DateTime.now();

    Duration duration = now.difference(this);

    if (duration.inMinutes <= 5) {
      return "刚刚";
    }

    if (duration.inMinutes <= 10) {
      return "5分钟前";
    }

    if (duration.inMinutes <= 30) {
      return "10分钟前";
    }

    if (duration.inHours < 1) {
      return "半小时前";
    }

    if (duration.inHours <= 12) {
      return "${duration.inHours}小时前";
    }

    // if (year == now.year && month == now.month && day == now.day) {
    //   // 同一天 显示时间
    //   return "${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}";
    // }

    if (year == now.year && month == now.month) {
      // 同年同月 显示月份日期
      return "${month.toString().padLeft(2, '0')}-${day.toString().padLeft(2, '0')}";
    }

    return "$year-${month.toString().padLeft(2, '0')}-${day.toString().padLeft(2, '0')}";
  }

  String get weekDay {
    var week = weekday;
    String w = '';
    switch (week.toString()) {
      case '1':
        w = '一';
        break;
      case '2':
        w = '二';
        break;
      case '3':
        w = '三';
        break;
      case '4':
        w = '四';
        break;
      case '5':
        w = '五';
        break;
      case '6':
        w = '六';
        break;
      case '7':
        w = '日';
        break;
    }
    return '星期$w';
  }

  /// 格式：yyyy 年 MM 月 dd 日 HH:mm
  String get formatDateTime {
    String timeString =
        "${year.toString()}年${month.toString()}月${day.toString()}日 ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}";
    return timeString;
  }

  // yyyyMMddhhmmss
  String get etime {
    String timeString =
        "${year.toString()}${month.toString().padLeft(2, '0')}${day.toString().padLeft(2, '0')}${hour.toString().padLeft(2, '0')}${minute.toString().padLeft(2, '0')}${second.toString().padLeft(2, '0')}";
    return timeString;
  }
}
